package cn.wzut.edut.controller;

import cn.wzut.edut.dao.pojo.table.PtyCollege;
import cn.wzut.edut.pojo.*;
import cn.wzut.edut.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    CollegeService collegeService;

    /**
     * 获取学院列表
     *
     * @return
     */
    @PostMapping("get_list")
    public JsonResult getCollegeList(HttpServletRequest request){
        String token = request.getHeader("token");
        List<PtyCollege> collegeList = collegeService.getCollegeList(token);
        if(collegeList != null){
            return new JsonResult(collegeList);
        }
        return new JsonResult(-1, "获取学院列表失败");
    }

    /**
     * 新增学院
     * @return
     */
    @PostMapping("add")
    public JsonResult addCollege(HttpServletRequest request, CollegeInfo collegeInfo){
        String token = request.getHeader("token");
        if(collegeService.addCollege(token, collegeInfo)){
            return new JsonResult(1, "添加学院成功");
        }
        return new JsonResult(-1, "添加学院失败");
    }

    /**
     * 删除学院
     * @return
     */
    @PostMapping("del")
    public JsonResult DelCollege(HttpServletRequest request, String[] collegeIds){
        String token = request.getHeader("token");
        if(collegeService.delCollege(token, collegeIds)){
            return new JsonResult(1, "删除学院成功");
        }
        return new JsonResult(-1, "删除学院失败");
    }

    /**
     * 修改学院
     * @return
     */
    @PostMapping("mod")
    public JsonResult modCollege(HttpServletRequest request, CollegeModInfo collegeModInfo){
        String token = request.getHeader("token");
        if(collegeService.modCollege(token, collegeModInfo)){
            return new JsonResult(1, "修改学院成功");
        }
        return new JsonResult(-1, "修改学院失败");
    }

    /**
     * 获取学院信息
     * @return
     */
    @PostMapping("get")
    public JsonResult getCollegeInf(HttpServletRequest request, String collegeId){
        String token = request.getHeader("token");
        CollegeInfo collegeInfo = collegeService.getCollegeById(token, collegeId);
        if(collegeInfo != null){
            return new JsonResult(collegeInfo);
        }
        return new JsonResult(-1, "获取学院信息失败");
    }

}
